package com.qding.park.manager.service.statis.biz;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.qding.park.common.core.cache.service.CacheServiceFacade;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.manager.adapter.factory.AdapterParkProxyFactory;
import com.qding.park.manager.cache.ConfigDataCacheInter;
import com.qding.park.manager.cache.ParkCacheInter;
import com.qding.park.manager.pojo.ReserveParams;
import com.qding.park.manager.pojo.TbReserve;
import com.qding.park.manager.pojo.enums.ReserverStatusEnum;
import com.qding.park.manager.service.statis.mapper.TbReserveStatisMapper;
import com.qding.park.manager.sms.SmsInterfaceServiec;

@Component("reserveBiz_statis")
public class ReserveBiz {
	private static final Log _log = LogFactory.getLog(ReserveBiz.class);

	@Autowired
	private AdapterParkProxyFactory adapterParkProxyFactory;

	@Autowired
	private CacheServiceFacade cacheServiceFacade;

	@Autowired
	private SmsInterfaceServiec smsInterfaceServiec;

	@Autowired
	private ConfigDataCacheInter configDataCacheInter;
	
	@Autowired
	private  ParkCacheInter  parkCacheInter;
	
	@Autowired
	private TbReserveStatisMapper  tbReserveMapper;


	/**
	 * @param reserve
	 *            预定单
	 * @return 得到当前预定状态
	 */
	public String getCurrentStatus(TbReserve reserve) {

		if (reserve.getStatus() == ReserverStatusEnum.RESERVER_PAYED_SUCCESS.getStatus()
				|| reserve.getStatus() == ReserverStatusEnum.RESERVER_ISSUED_PARKSUCCESS.getStatus()

		) {

			return "待进场";
		} else if (reserve.getStatus() == ReserverStatusEnum.RESERVER_INCHECK.getStatus()
				|| reserve.getStatus() == ReserverStatusEnum.RESERVER_DELAY_TIMEOUT.getStatus()
				|| reserve.getStatus() == ReserverStatusEnum.RESERVER_OVERTIME_INCHECK.getStatus()) {

			String checkInTimeStr = reserve.getCheckInTime() == null ? ""
					: DateUtils.formatDate(reserve.getCheckInTime(), DateUtils.DATE_FORMAT_DATETIME);
			return checkInTimeStr + " 已入场";
		} else if (reserve.getStatus() == ReserverStatusEnum.RESERVER_CANCEL.getStatus()
//				|| reserve.getStatus() == ReserverStatusEnum.RESERVER_OVERTIME.getStatus()
				|| reserve.getStatus() == ReserverStatusEnum.RESERVER_ISSUED_PARKFail.getStatus()) {
			String timeStr = reserve.getUpdate_date() == null ? ""
					: DateUtils.formatDate(reserve.getUpdate_date(), DateUtils.DATE_FORMAT_DATETIME);
			return timeStr+" 已取消";
		}else if(reserve.getStatus() == ReserverStatusEnum.RESERVER_OVERTIME.getStatus()){
			String timeStr = reserve.getEndTime() == null ? ""
					: DateUtils.formatDate(reserve.getEndTime(), DateUtils.DATE_FORMAT_DATETIME);
			return timeStr+" 已过期";
		}else if(reserve.getStatus() == ReserverStatusEnum.DEL.getStatus()){
			return "用户端删除";
		}
		
		else {
			return "";
		}
	}

	public int getOrderFee(TbReserve reserve) {
		if (reserve.getStatus() == ReserverStatusEnum.RESERVER_PAYED_SUCCESS.getStatus()
				|| reserve.getStatus() == ReserverStatusEnum.RESERVER_INCHECK.getStatus()
				|| reserve.getStatus() == ReserverStatusEnum.RESERVER_OVERTIME.getStatus()
				|| reserve.getStatus() == ReserverStatusEnum.RESERVER_OVERTIME_INCHECK.getStatus()
				|| reserve.getStatus() == ReserverStatusEnum.RESERVER_ISSUED_PARKSUCCESS.getStatus()) {

			return reserve.getDeposit();

		} else {
			return 0;
		}
	}
	
	
	public String getOrderStatusStr(TbReserve reserve) {
		String temp = null;

		String createTimestr = DateUtils.formatDate(reserve.getCreate_date(), DateUtils.DATE_FORMAT_DATETIME);
		createTimestr = createTimestr + " ";
		String tag = "成功";
		if (reserve.getStatus() == ReserverStatusEnum.RESERVER_PAYED_SUCCESS.getStatus()
				|| reserve.getStatus() == ReserverStatusEnum.RESERVER_INCHECK.getStatus()
				|| reserve.getStatus() == ReserverStatusEnum.RESERVER_CANCEL.getStatus()
				|| reserve.getStatus() == ReserverStatusEnum.RESERVER_OVERTIME.getStatus()
				|| reserve.getStatus() == ReserverStatusEnum.RESERVER_OVERTIME_INCHECK.getStatus()
				|| reserve.getStatus() == ReserverStatusEnum.RESERVER_ISSUED_PARKSUCCESS.getStatus()
				|| reserve.getStatus() == ReserverStatusEnum.DEL.getStatus()) {
			tag = "成功";
		} else {
			tag = "失败";
		}
		temp = createTimestr + tag;
		return temp;
	}
	
	public ReserveParams queryReserveParams(int parkId) {

        ReserveParams params = tbReserveMapper.reserveParams(parkId);
        return params;
    }
	
	public void setReserveParams(ReserveParams params, int parkId) {
        tbReserveMapper.updateReserveParams(params, parkId);
        parkCacheInter.clear(parkId);
    }
}
